<?php
namespace app\api\controller\driver;
use app\api\model\DriverAccount;
use think\Controller;
use app\api\model\Driver as DriverModel;
/**
 * @name【司机账号管理】
 * @baseurl【driver.index】
 */
class Account extends Base
{
    /**
     * @name【添加银行卡】
     * @desc【添加银行卡】
     * @uri【addCard】
     * @method【post】
     * {参数名,类型,默认值,描述,必传}
     * @param【
     * dtoken $ string $ abcd $ 校验码 $ 1 #
     * bank_name $ string $ 工商银行 $ 银行名称 $ 1#
     * bank_open_addr $ string $ 中国 $ 开户行#
     * bank_number $ string $ 6229 $ 卡号$1#
     * bind_phone $ string $ 13888888888 $ 绑定手机 $ 1#
     * real_name $ string $ 张三 $ 姓名 $ 1
     * 】
     * {参数名,类型,mock,描述,默认值}
     * @return【
     * code $ int $ number|1-1 $ 校验码#
     * msg $ string $  $ 错误信息
     * 】
     */
    public function addCard(){
        $this->auth();
        $data['driver_id']      = $this->driverInfo['driver_id'];
        $data['bank_name']      = input('bank_name');
        $data['bank_number']    = input('bank_number');
        $data['card_type']      = input('card_type',0);
        $data['bank_open_addr'] = input('bank_open_addr','');
        $data['bind_phone']     = input('bind_phone');
        $data['real_name']      = input('real_name');
        $driverCard             = new \app\api\model\DriverCard();
        if($driverCard->validate(true)->save($data)){
            return $this->retSucc();
        }else{
            return $this->retError($driverCard->getError());
        }
    }
    
    /**
     * @name【银行卡详情】
     * @desc【银行卡详情】
     * @uri【cardDetail】
     * @method【post】
     * @param【
     * dtoken $ string $ abcd $ 校验码 $ 1#
     * driver_card_id $ int $ 0 $ 银行卡id $ 1
     * 】
     * @return【
     * code $ int $ number|1-1 $ 校验码#
     * msg $ string $  $ 错误信息#
     * data $ string $  $ 银行卡详情 $ {"real_name":"中国","bank_name":"111","bank_number":"6229090","bank_open_addr":"泉州","bind_phone":"13959203611"}
     * 】
     */
    public function cardDetail(){
        $this->auth();
        $where['driver_id']      = $this->driverInfo['driver_id'];
        $where['driver_card_id'] = input('driver_card_id',2);
        $field                   = 'real_name,bank_name,bank_number,bank_open_addr,bind_phone';
        $data['data']            = (new \app\api\model\DriverCard)->where($where)->field($field)->find();
        if(empty($data['data'])){
            return $this->retError(109);
        }else{
            return $this->retSucc($data);
        }
    }
    
    /**
     * @name【编辑银行卡】
     * @desc【编辑银行卡】
     * @uri【editCard】
     * @method【post】
     * @param【
     * dtoken $ string $ abcd $ 校验码 $ 1#
     * driver_card_id $ int $ 0 $ 银行卡id $ 1
     * 】
     * @return【
     * code $ int $ number|1-1 $ 校验码#
     * msg $ string $  $ 错误信息
     * 】
     */
    public function editCard(){
        $this->auth();
        $where['driver_id']     = $this->driverInfo['driver_id'];
        $where['driver_card_id']= input('driver_card_id',0);
        $data['bank_name']      = input('bank_name');
        $data['bank_number']    = input('bank_no');
        $data['card_type']      = input('card_type',0);
        $data['bank_open_addr'] = input('bank_open_addr','');
        $data['bind_phone']     = input('bind_phone');
        $data['real_name']      = input('real_name');
        $driverCard              = new \app\api\model\DriverCard();
        if($driverCard->save($data,$where)){
            return $this->retSucc();
        }else{
            return $this->retError($driverCard->getError());
        }
    }
    
    /**
     * @name【删除银行卡】
     * @desc【删除银行卡】
     * @uri【delCard】
     * @method【post】
     * @param【
     * dtoken $ string $ abcd $ 校验码 $ 1#
     * driver_card_id $ int $ 0 $ 银行卡id $ 1
     * 】
     * @return【
     * code $ int $ number|1-1 $ 校验码#
     * msg $ string $  $ 错误信息
     * 】
     */
    public function delCard(){
        $this->auth();
        $where['driver_id']      = $this->driverInfo['driver_id'];
        $where['driver_card_id'] = input('driver_card_id',0);
        if(\app\api\model\DriverCard::destroy($where)){
            return $this->retSucc();
        }else{
            return $this->retError(109);
        }
    }
    /**
     * @name【银行卡列表】
     * @desc【银行卡列表】
     * @uri【cardList】
     * @method【post】
     * @param【
     * dtoken $ string $ abcd $ 校验码 $ 1#
     * page   $ int $ 1 $ 页数 #
     * pagenum $ num $ 10 $ 记录数
     * 】
     * @return【
     * code $ int $ number|1-1 $ 校验码#
     * msg $ string $  $ 错误信息#
     * data $ string $  $ 银行卡列表 $ [{"driver_card_id":2,"real_name":"中国","bank_name":"111","bank_number":"sadfasdfasdffffffffffffff","bank_open_addr":"","bind_phone":null},
     * {"driver_card_id":2,"real_name":"中国","bank_name":"111","bank_number":"sadfasdfasdffffffffffffff","bank_open_addr":"","bind_phone":null}]
     * 】
     */
    public function cardList(){
        $this->auth();
        $where['driver_id']      = $this->driverInfo['driver_id'];
        $field                   = 'driver_card_id,real_name,bank_name,bank_number,bank_open_addr,bind_phone';
        $data['data']            = (new \app\api\model\DriverCard)->where($where)->field($field)->page(input('page',1),input('pagenum',10))->select();
        if(empty($data['data'])){
            return $this->retError(110);
        }else{
            return $this->retSucc($data);
        }
    }
    /**
     * @name【设置提现密码】
     * @desc【设置提现密码】
     * @uri【setWithdrawPass】
     * @method【post】
     * @param【
     * dtoken $ string $ abcd $ 校验码 $ 1#
     * withdraw_password $ string $ abcd $ 提现密码 $ 1 #
     * old_password $ string $ 123 $ 旧提现密码
     * 】
     * @return【
     * code $ int $ number|1-1 $ 校验码#
     * msg $ string $  $ 错误信息
     * 】
     */
    public function setWithdrawPass(){
        $this->auth();
        $driverAccount                  = new \app\api\model\DriverAccount();
        $account                        = DriverAccount::get(['driver_id'=>$this->driverInfo['driver_id']]);
        $where['driver_id']             = $this->driverInfo['driver_id'];
        if(!empty($account) && $account['withdraw_password']!=input('old_password')){
            return $this->retError(117);
        }
        if(input('old_password')){
            $where['withdraw_password'] = input('old_password');
        }
        $data['withdraw_password'] = input('withdraw_password');

        if($driverAccount->validate(true)->save($data,$where)){
            return $this->retSucc();
        }else{
            return $this->retError($driverAccount->getError());
        }
    }
    /**
     * @name【增加提现】
     * @desc【增加提现】
     * @uri【addWithdraw】
     * @method【post】
     * @param【
     * dtoken $ string $ abcd $ 校验码 $ 1#
     * price $ int $ 99 $ 提现金额 $ 1 #
     * car_id $ int $ 1 $ 银行卡id $ 1#
     * password $ string $ 123456 $ 提现密码
     * 】
     * @return【
     * code $ int $ number|1-1 $ 校验码#
     * msg $ string $  $ 错误信息
     * 】
     */
    public function addWithdraw(){
        $this->auth();
        $data['number']     = 'TX'.date('YmdHis').(10000000-$this->driverInfo['driver_id']);
        $data['driver_id']  = $this->driverInfo['driver_id'];
        $data['price']      = input('price',10);
        $data['card_id']    = input('car_id');
        $data['withdrawals_state']  = 1;
        $where['driver_id'] = $data['driver_id'];
        $where['withdraw_password'] = input('password');
        $balance            = \app\api\model\DriverAccount::where($where)->value('balance');
        if(!$balance){
            return $this->retError(116);
        }
        if($balance<$data['price']){
            return $this->retError(111);
        }
        $data['apply_date'] = date('Y-m-d H:i:s');
        $WithdrawalsRecord  = new \app\api\model\WithdrawalsRecord();
        if($WithdrawalsRecord->validate(true)->save($data)){
            return $this->retSucc();
        }else{
            return $this->retError($WithdrawalsRecord->getError());
        }

    }
    /**
     * @name【删除提现】
     * @desc【删除提现】
     * @uri【delWithdraw】
     * @method【post】
     * @param【
     * dtoken $ string $ abcd $ 校验码 $ 1#
     * withdrawals_record_id $ int $ 0 $ 提现id $ 1
     * 】
     * @return【
     * code $ int $ number|1-1 $ 校验码#
     * msg $ string $  $ 错误信息
     * 】
     */
    public function delWithdraw(){
        $this->auth();
        $where['driver_id']             = $this->driverInfo['driver_id'];
        $where['withdrawals_record_id'] = input('withdrawals_record_id',0);
        if(\app\api\model\WithdrawalsRecord::destroy($where)){
            return $this->retSucc();
        }else{
            return $this->retError(112);
        }
    }
    /**
     * @name【提现列表】
     * @desc【提现列表】
     * @uri【listWithdraw】
     * @method【post】
     * @param【
     * dtoken $ string $ abcd $ 校验码 $ 1#
     * page   $ int $ 1 $ 页数 #
     * pagenum $ num $ 10 $ 记录数
     * 】
     * @return【
     * code $ int $ number|1-1 $ 校验码#
     * msg $ string $  $ 错误信息#
     * data $ string $ $ 提现列表$[{"withdrawals_record_id":4,"number":"TX201801202225199999983","price":"0.00","apply_date":"2018-01-20 22:25:19","withdrawals_state":null,"pay_date":null},
     * {"withdrawals_record_id":5,"number":"TX201801202227019999983","price":"10.00","apply_date":"2018-01-20 22:27:01","withdrawals_state":"1","pay_date":null}]
     * 】
     */
    public function listWithdraw(){
        $this->auth();
        $where['driver_id']      = $this->driverInfo['driver_id'];
        $field                   = 'withdrawals_record_id,number,price,apply_date,withdrawals_state,pay_date';
        $data['data']            = (new \app\api\model\WithdrawalsRecord)->where($where)->field($field)->page(input('page',1),input('pagenum',10))->select();
        if(empty($data['data'])){
            return $this->retError(110);
        }else{
            return $this->retSucc($data);
        }
    }
    /**
     * @name【提现查看】
     * @desc【提现查看】
     * @uri【viewWithdraw】
     * @method【post】
     * @param【
     * dtoken $ string $ abcd $ 校验码 $ 1#
     * withdrawals_record_id $ int $ 0 $ 提现ID $ 1
     * 】
     * @return【
     * code $ int $ number|1-1 $ 校验码#
     * msg $ string $  $ 错误信息#
     * data $ string $ $ 提现列表${"withdrawals_record_id":4,"number":"TX201801202225199999983","price":"0.00","apply_date":"2018-01-20 22:25:19","withdrawals_state":null,"pay_date":null}
     * 】
     */
    public function viewWithdraw(){
        $this->auth();
        $where['driver_id']             = $this->driverInfo['driver_id'];
        $where['withdrawals_record_id'] = input('withdrawals_record_id',0);
        $field                          = 'number,price,apply_date,withdrawals_state,pay_date';
        $data['data']                   = (new \app\api\model\WithdrawalsRecord)->where($where)->field($field)->find();
        if(empty($data['data'])){
            return $this->retError(113);
        }else{
            return $this->retSucc($data);
        }
    }
  
    /**
     * @name【意见反馈】
     * @desc【意见反馈】
     * @uri【addFeedback】
     * @method【post】
     * @param【
     * dtoken $ string $ abcd $ 校验码 $ 1#
     * content $ string $ 意见内容 $ 意见内容 $ 1
     * 】
     * @return【
     * code $ int $ number|1-1 $ 校验码#
     * msg $ string $  $ 错误信息
     * 】
     */
    public function addFeedback(){
        $this->auth();
        $driver_id              = $this->driverInfo['driver_id'];
        $driver                 = DriverModel::get($driver_id);
        $data['user_id']        = $driver->user_id;
        $data['content']        = input('content');
        $data['feedback_time']  = date('Y-m-d H:i:s');
        $Feedback               = new \app\api\model\Feedback();
        if($Feedback->validate(true)->save($data)){
            return $this->retSucc();
        }else{
            return $this->retError($Feedback->getError());
        }
    }
    
    /**
     * @name【反馈列表】
     * @desc【反馈列表】
     * @uri【listFeedback】
     * @method【post】
     * @param【
     * dtoken $ string $ abcd $ 校验码 $ 1#
     * page   $ int $ 1 $ 页数 #
     * pagenum $ num $ 10 $ 记录数
     * 】
     * @return【
     * code $ int $ number|1-1 $ 校验码#
     * msg $ string $  $ 错误信息#
     * data $ string $ $ 列表$[{"feedback_id":3,"feedback_time":"2018-01-21 00:04:49","content":"1"},{"feedback_id":3,"feedback_time":"2018-01-21 00:04:49","content":"1"}]
     * 】
     */
    public function listFeedback(){
        $this->auth();
        $driver_id              = $this->driverInfo['driver_id'];
        $driver                 = DriverModel::get($driver_id);
        $where['user_id']       = $driver->user_id;
        $field                  = 'feedback_id,feedback_time,content';
        $data['data']           = (new \app\api\model\Feedback())->where($where)->field($field)->page(input('page',1),input('pagenum',10))->select();
        if(empty($data['data'])){
            return $this->retError(110);
        }else{
            return $this->retSucc($data);
        }
    }
    /**
     * @name【反馈查看】
     * @desc【反馈查看】
     * @uri【viewFeedback】
     * @method【post】
     * @param【
     * dtoken $ string $ abcd $ 校验码 $ 1#
     * feedback_id $ int $ 0 $ 反馈id $ 1
     * 】
     * @return【
     * code $ int $ number|1-1 $ 校验码#
     * msg $ string $  $ 错误信息#
     * data $ string $ $ 列表${"feedback_time":"2018-01-21 00:04:49","content":"1"}
     * 】
     */
    public function viewFeedback(){
        $this->auth();
        $driver_id              = $this->driverInfo['driver_id'];
        $driver                 = DriverModel::get($driver_id);
        $where['user_id']       = $driver->user_id;
        $where['feedback_id']   = input('feedback_id',0);
        $field                  = 'feedback_time,content';
        $data['data']           = (new \app\api\model\Feedback())->where($where)->field($field)->find();
        if(empty($data['data'])){
            return $this->retError(113);
        }else{
            return $this->retSucc($data);
        }
    }

    /**
     * @name【余额查询】
     * @desc【余额查询】
     * @uri【getBlance】
     * @method【post】
     * @param【
     *  dtoken $ string $ abcd $ 校验码 $ 1
     * 】
     * @return【
     * code $ int $ number|1-1 $ 校验码 #
     * msg $ string $  $ 错误信息 #
     * balance $ float $ $ 余额
     * 】
     */
    public function getBlance(){
        $this->auth();
        $driver = DriverAccount::get(['driver_id'=>$this->driverInfo['driver_id']]);
        $money  = $driver->balance ? $driver->balance :0 ;
        return $this->retSucc(['balance'=>$money]);
    }
}